package com.st.bottomnavigation.utils;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

public class Data {
    public static String getString(JSONObject json, String key, String def){
        String ret = json.containsKey(key) ? json.getString(key) : null;
        if(ret==null) return def;
        else return ret;
    }

    public static Integer getInteger(JSONObject json,String key,Integer def){
        Integer ret = json.containsKey(key) ? json.getInteger(key) : null;
        if(ret==null) return def;
        else return ret;
    }

    public static Long getLong(JSONObject json,String key,Long def){
        Long ret = json.containsKey(key) ? json.getLong(key) : null;
        if(ret==null) return def;
        else return ret;
    }

    public static JSONArray getJSONArray(JSONObject json, String key, JSONArray def){
        JSONArray ret = json.containsKey(key) ? json.getJSONArray(key) : null;
        if(ret==null) return def;
        else return ret;
    }
    public static JSONObject getJSONObject(JSONObject json, String key, JSONObject def){
        JSONObject ret = json.containsKey(key) ? json.getJSONObject(key) : null;
        if(ret==null) return def;
        else return ret;
    }
    public static boolean isNull(String str){
        return str==null || "".equals(str.trim());
    }

    public static boolean isNotNull(String str){
        if(str==null) return false;
        if("".equals(str.trim())) return false;
        return true;
    }

    public static boolean isJson(String str){
        if(isNull(str)) return false;
        str = str.trim();
        if(!str.startsWith("{") || !str.endsWith("}")) return false;
        return true;
    }

    /**
     * JSONArray不为空判断
     * @param list
     * @return boolean
     */
    public static boolean isNotEmptyJSONArray(JSONArray list) {
        return list != null && list.size() > 0;
    }

    public static String handleNowDate(String inoutTime) {
        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
            if (StringUtils.isNotEmpty(inoutTime)) {
                LocalDateTime dateTime = XDate.StringToDate(inoutTime);
                LocalDate today = LocalDate.now();
                // 判断是否是今天
                if (dateTime.toLocalDate().equals(today)) {
                    // 如果是今天，截取小时、分钟和秒部分
                    inoutTime = dateTime.format(DateTimeFormatter.ofPattern("HH:mm:ss"));
                } else {
                    inoutTime = XDate.truncateYear(inoutTime);
                }
            }
        }
        return inoutTime;
    }

}
